#include "common_api.h"



void ADC_init(void)
{
		uint8_t t;
		/* Power on ADC module */
		ADC_POWER_ON(ADC);

		/* Set the ADC operation mode as single, input mode as single-end and enable the analog input channel 2 */
		ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE_CYCLE, (0x1 << OILLEVEL_CH )| (0x1 << OILTEM_CH));
		ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
		//ADC中断部分是否使能
		/* Clear the A/D interrupt flag for safe */
//		ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
//		/* Enable the ADC interrupt */
//		ADC_EnableInt(ADC, ADC_ADF_INT);
//		NVIC_EnableIRQ(ADC_IRQn);
	
//		ADC_START_CONV(ADC); 
		
		for(t=0;t<32;t++)
		{
			Measure_OilLevel();
			Measure_OilTem();
		}

}

uint16_t AD_Filter(uint8_t channal)
{
	u16 temp;
	ADC_START_CONV(ADC);	
	temp = ADC_GET_CONVERSION_DATA(ADC, channal);
	temp = temp >> 2;
	return temp;
}


void Measure_OilLevel(void)
{
	
    MeasureAD[OILLEVEL_SN].sum -= MeasureAD[OILLEVEL_SN].buf[MeasureAD[OILLEVEL_SN].cnt];
    MeasureAD[OILLEVEL_SN].buf[MeasureAD[OILLEVEL_SN].cnt] = (AD_Filter (OILLEVEL_CH));
    MeasureAD[OILLEVEL_SN].sum += MeasureAD[OILLEVEL_SN].buf[MeasureAD[OILLEVEL_SN].cnt++];
    OilLevelAD = MeasureAD[OILLEVEL_SN].sum >> 3;
	//测试数据多少
//	LCD_ShowxNum(100, 80, OilLevelAD, 4, 48, 0x80); //高位补零
}

void Measure_OilTem(void)
{

    MeasureAD[OILTEM_SN].sum -= MeasureAD[OILTEM_SN].buf[MeasureAD[OILTEM_SN].cnt];
    MeasureAD[OILTEM_SN].buf[MeasureAD[OILTEM_SN].cnt] = (AD_Filter (OILTEM_CH));
    MeasureAD[OILTEM_SN].sum += MeasureAD[OILTEM_SN].buf[MeasureAD[OILTEM_SN].cnt++];
    OilTemAD = MeasureAD[OILTEM_SN].sum >> 3;

}
void Measure_WaterTem(void)
{

    MeasureAD[WATERTEMP_SN].sum -= MeasureAD[WATERTEMP_SN].buf[MeasureAD[WATERTEMP_SN].cnt];
    MeasureAD[WATERTEMP_SN].buf[MeasureAD[WATERTEMP_SN].cnt] = (AD_Filter (WATERTEMP_CH));
    MeasureAD[WATERTEMP_SN].sum += MeasureAD[WATERTEMP_SN].buf[MeasureAD[WATERTEMP_SN].cnt++];
    WaterTemAD = MeasureAD[WATERTEMP_SN].sum >> 3;

}
void Measure_Step(void)
{
	uint16_t Oil_Temp,Water_Temp;
	//油位分档，线性平滑处理
	if(OilLevelAD >= 610) 
	{
		MotorA.TarGet = Motor_Range;
	}
	else if(OilLevelAD >=550)
	{
		Oil_Temp = (OilLevelAD -550)*Motor_Range/240; 	//等比例	(OilLevelAD -550)/60 * Motor_Range/4
		MotorA.TarGet =  Motor_Range*3/4 + Oil_Temp;
	}
	else if(OilLevelAD >=450)
	{
		Oil_Temp = (OilLevelAD -450)*Motor_Range/400; 	//等比例	(OilLevelAD -450)/100 * Motor_Range/4
		MotorA.TarGet =  Motor_Range/2 + Oil_Temp;
	}
	else if(OilLevelAD >=225)
	{
		Oil_Temp = (OilLevelAD -225)*Motor_Range/900; 	//等比例	(OilLevelAD -225)/225 * Motor_Range/4
		MotorA.TarGet =  Motor_Range/4 + Oil_Temp;
	}
	else if(OilLevelAD >=75)
	{
		Oil_Temp = (OilLevelAD -75)*Motor_Range/600; 	//等比例  (OilLevelAD -75)/150 * Motor_Range/4
		MotorA.TarGet =  Oil_Temp;
	}	
	else 
	{
		MotorA.TarGet =0; 
	}			
	
	//水温分档，线性平滑处理,档位划分方案待确定
	if(WaterTemAD >= 810) 
	{
		MotorB.TarGet = 0;
	}
	else if(WaterTemAD >=710)
	{
		Water_Temp = (WaterTemAD -710)*Motor_Range/200; 	//等比例	(WaterTemAD -710)/100 * Motor_Range/2
		MotorB.TarGet =   Water_Temp;
	}
	else if(WaterTemAD >=550)	// 1/2 + 3/8
	{
		Water_Temp = (WaterTemAD -550)*Motor_Range *3/1280; 	//等比例	(WaterTemAD -550)/160 * Motor_Range *3/8
		MotorB.TarGet =  Motor_Range/2 + Water_Temp;
	}
	else if(WaterTemAD >=510)
	{
		Water_Temp = (WaterTemAD -510)*Motor_Range /320; 	//等比例	(WaterTemAD -510)/40 * Motor_Range/8
		MotorB.TarGet =  Motor_Range * 7/8 + Water_Temp;
	}
	else 
	{
		MotorB.TarGet = Motor_Range; 
	}				

}

















